﻿package cn.antscrpt.antLib.Effects 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	/**
	 * ...
	 * @author AntScript
     * @blog  http://www.antscript.cn
	 */
	public class MouseParticle extends Sprite
	{
		
		private var _target :DisplayObject;
		private var _xOffset:Number;
		private var _yOffset:Number;
		
		private var _bgWidth:int;
		private var _bgHeight:int;
		
		private var _particles:Vector.<Particle>;
		
		private var _bitmap:Bitmap;
		private var _canvas:BitmapData;
		
		
		
		public function MouseParticle(displayObject:DisplayObject,bgWidth:Number,bgHeight:Number,xOffset:Number=0,yOffset:Number=0) 
		{
			_target = displayObject;
			
			_bgWidth = bgWidth;
			_bgHeight = bgHeight;
			
			_xOffset = xOffset;
			_yOffset = yOffset;
			
			initParticles();
		}
		
		private function initParticles():void
		{
			var bitmapdata:BitmapData = new BitmapData(_bgWidth, _bgHeight, true, 0x00000000);
			var matrix:Matrix = new Matrix(1, 0, 0, 1, (_bgWidth - _target.width) / 2 - _xOffset, (_bgHeight - _target.height) / 2 - _yOffset);
			bitmapdata.draw(_target, matrix);
			
			_particles = Vector.<Particle>([]);
			
			var xLoop:int = _bgWidth;
			var yLoop:int = _bgHeight;
			
			for (var i:int = 0; i < xLoop; i++) 
			{
				for (var j:int = 0; j < yLoop; j++) 
				{
					var color:uint = bitmapdata.getPixel32(i, j);
					
					if ((color >> 24) > 0) {
						var particle:Particle = new Particle(i, j, color);
						
						_particles.push(new Particle(i, j, color));
					}
				}
			}
			
		}
		
		private function setParticle(p:Particle):void {
			p.x = mouseX + (Math.random() * 50 - 25);
			p.y = mouseY + (Math.random() * 50 - 25);
			p.vx = Math.random() * 20 - 10;
			p.vy = Math.random() * 20 - 10;
		}
		
	}
	
}

class Particle {
	
	public var x:Number;
	public var y:Number;
	public var targetX:int;
	public var targetY:int;
	public var vx:Number;
	public var vy:Number;
	public var ax:Number;
	public var ay:Number;
	public var color:uint;
	
	public function Particle(tX:Number,tY:Number,c:uint=0xffffffff) {
		targetX = tX;
		targetY = tY;
		color = c;
	}
}